package org.example.j2025_3_3.controller;

import org.example.j2025_3_3.mapper.UserMapper;
import org.example.j2025_3_3.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper ;

    @RequestMapping("/login")
    public boolean login(UserInfo userInfo , HttpSession session){
        if(!StringUtils.hasLength(userInfo.getName()) || !StringUtils.hasLength(userInfo.getPassword())){
            return false ;
        }

       String ps = userMapper.getPassword(userInfo.getName()) ;
        if(userInfo.getPassword().equals(ps)){
            userInfo.setPassword("");
            session.setAttribute("USER_KEY",userInfo);
            return true ;
        }

        return false ;
    }
}
